﻿using Newtonsoft.Json;
using System.IO;
using System.Security.AccessControl;
using System;
using System.Text;

namespace CarparkIMS_CMS.Models
{
    public class StdApiUrlConfig
    {
        /// <summary>
        /// Http Host Url http://wwww.123.com/
        /// </summary>
        public virtual string BaseAddress
        {
            get
            {
                //string _BaseAddress = $"​{Schema}://{Host}:{Port}";
                //if(IPAddress.TryParse(Host,out IPAddress address))
                //{
                //    _BaseAddress = $"​{address.ToString()}:{Port}";
                //    return _BaseAddress;
                //}
                UriBuilder uriBuilder = new UriBuilder(Schema, Host, Port);
                string _BaseAddress = uriBuilder.Uri.ToString();
                return _BaseAddress;
            }
        }
         
        /// <summary>
        /// http Or https 
        /// </summary>
        private string _Schema;
        /// <summary>
        /// http Or https
        /// </summary>
        public string Schema
        {
            get
            {
                _Schema = _Schema.ToLower();

                if (_Schema == "http")
                {
                    return "http";
                }
                else
                {
                    return "https";
                }
            }
            set
            {
                _Schema = value;
            }
        }
        private string _Host;
        /// <summary>
        /// 192.168.1.14  Or www.abcdrf.com
        /// </summary>
        public string Host
        {
            get
            {
                return _Host;
            }
            set
            {
                _Host = value;
            }
        }
        private int _Port;
        /// <summary>
        /// 80, 8080 
        /// </summary>
        public int Port
        {
            get
            {
                return _Port;
            }
            set
            {
                _Port = value;
            }
        }
        private string _UserName;
        public string UserName
        {
            get
            {
                return _UserName;
            }
            set
            {
                _UserName = value;
            }
        }
        private string _Password;
        public string Password
        {
            get
            {
                return _Password;
            }
            set
            {
                _Password = value;
            }
        }

        public static StdApiUrlConfig Get()
        {
            string apiUrlConfigJosn;
            string pathFileName = Path.Combine(Directory.GetCurrentDirectory(), "ApiUrlConfig.json");
            apiUrlConfigJosn = ReadConfigJson(pathFileName);

            StdApiUrlConfig apiUrlConfig = JsonConvert.DeserializeObject<StdApiUrlConfig>(apiUrlConfigJosn);
            return apiUrlConfig;
        }

        public string UrlCombine(string relatePath)
        {
            if (string.IsNullOrEmpty(BaseAddress))
                return string.Empty;
            if (string.IsNullOrEmpty(relatePath))
                return string.Empty;

            relatePath = relatePath.TrimStart('/');

            string url = $"{BaseAddress}{relatePath}";

            return url;
        }
        public static string ReadDataFromJson(string path, string targetFileName)
        {
            string PathFileName = Path.Combine(path, targetFileName);
            if (File.Exists(PathFileName))
            {
                string content = File.ReadAllText(PathFileName, Encoding.UTF8);
                return content;
            }
            else
            {
                return string.Empty;
            }
        }
        public static string ReadDataFromJson(string PathFileName)
        {
            if (File.Exists(PathFileName))
            {
                string content = File.ReadAllText(PathFileName, Encoding.UTF8);
                return content;
            }
            else
            {
                return string.Empty;
            }
        }
        public static string ReadConfigJson(string PathFileName)
        {
            if (File.Exists(PathFileName))
            {
                string content = File.ReadAllText(PathFileName, Encoding.UTF8);
                return content;
            }
            else
            {
                return string.Empty;
            }

        }
    }
}
